package com.wms.presenters;

import java.sql.SQLException;
import java.util.List;
import com.gatech.wms.R;
import com.wms.assets.User;
import com.wms.helper.DialogFactory;
import com.wms.helper.UserException;
import com.wms.model.LoginModel;
import android.os.Bundle;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * The loose presenter for the MainMenu screen BEFORE login.
 * @author Nathan
 */
public class LoginActivity extends WMSActivity {
	public static final int LOGIN_SUCCESS = 1;
	private LoginModel mModel;
	private DialogFactory dFactory;
	private static int FAIL_COUNT = 0;
	private List<User> suspended;
	
	/**
	 * This method initializes the screen and instantiates all necessary helper classes
	 * (like the Model).
	 *
	 *@param savedInstanceState
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_screen);
		dFactory = new DialogFactory(this);
		mModel = new LoginModel();
		try {
			suspended = mModel.getSuspendedUserList();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		setupButton();
	}
	
	private boolean hasName(String username) {
		for (User u : suspended) {
			if (u.getUsername().equals(username)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * This method gets a reference to the buttons and sets click listeners for every button
	 */
	private void setupButton() {
		
		Button login = (Button)findViewById(R.id.button1);  
		
		login.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				EditText user = (EditText)findViewById(R.id.editText1);
				String username = user.getText().toString();
				EditText pw = (EditText)findViewById(R.id.editText2);
				
				String password = pw.getText().toString();
				try {
					User ret = mModel.authentication(username, password);
					if (ret != null && hasName(ret.getUsername())) {
						AlertDialog msg = dFactory.alertOK("Your account has been blocked. Please contact an administrator for further assistance.", "Error!");
						msg.show();
					} else if (LoginActivity.FAIL_COUNT == 2) {
						mModel.suspendUser(username);
						AlertDialog msg = dFactory.alertOK("Maximum number of Logins Exceeded. Your account will be blocked.", "Error!");
						msg.show();						
					} 
					else if (ret != null) {
						if (ret.getRole().equals("admin"))	
							setResult(1);
						else
							setResult(-1);
						getUserModel().loginUser(ret);
						finish();
		        		
					} else {
						LoginActivity.FAIL_COUNT++;
						AlertDialog msg = dFactory.alertOK("Invalid Username or Password", "Error!");
						msg.show();
					}
				} catch (SQLException e) {
					AlertDialog msg = dFactory.alertOK("Cannot establish network connection! Please connect to a network and try again.", "Error!");
					msg.show();
				} catch (UserException e) {
					AlertDialog msg = dFactory.alertOK("Username not found!", "Error!");
					msg.show();
				} catch (Exception e) {
					e.printStackTrace();
				} 
			}
		});
		
		Button register = (Button)findViewById(R.id.button2);  
		
		register.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(LoginActivity.this, RegisterActivity.class);
				startActivityForResult(i, 0);
			}
		});
	}
}
